define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'warehouse/inventorylist/index',
                    add_url: 'warehouse/inventorylist/add',
                    multi_url: 'warehouse/inventorylist/multi',
                    record_url: 'warehouse/inventorylist/record',
                    table: 'inventory_list',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'itemname', title: __('Itemame')},
                        {field: 'sku', title: __('Sku')},
                        {field: 'inventorycount', title: __('Inventorycount'),operate:false},
                        {field: 'instoragetime', title: __('Instoragetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'outstoragetime', title: __('Outstoragetime'), operate:false, addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'warehouseid', title: __('Warehouseid'),operate:false,formatter:Controller.api.formatter.checkstatus},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'detail',
                                    title: __('商品出入库记录'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'warehouse/inventorylist/record'
                                }
                            ],
                            formatter: Table.api.formatter.operate}
                    ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                exportDataType: 'selected',
                exportTypes: ['doc', 'excel']
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            $(document).on('click','.in-warehouse',function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if(ids.length == 0){
                    Layer.confirm(
                        __('No in item', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        ''
                    );
                }else {
                    var url = "warehouse/inventorylist/inWarehouse";
                        ids = ($.isArray(ids) ? ids.join(",") : ids);
                        url = Table.api.replaceurl(url, {ids:ids}, table);
                    Fast.api.open(url, '入库', $(that).data() || {});
                }
            });

            $(document).on('click','.out-warehouse',function () {
                var that = this;
                var ids = Table.api.selectedids(table);
                if(ids.length == 0){
                    Layer.confirm(
                        __('No out item', ids.length),
                        {icon: 3, title: __('Warning'), offset: 0, shadeClose: true},
                        ''
                    );
                }else {
                    var url = "warehouse/inventorylist/outWarehouse";
                    ids = ($.isArray(ids) ? ids.join(",") : ids);
                    url = Table.api.replaceurl(url, {ids:ids}, table);
                    Fast.api.open(url, '出库', $(that).data() || {});
                }
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {

            Controller.api.bindevent();
        },
        inwarehouse:function () {

            $(document).on('click','.del-item',function () {
                if($(".pd-item").length > 1){
                    $(this).parent().remove();
                }else {
                    $(this).remove();
                }
            });
            Controller.api.bindevent();
        },
        outwarehouse: function () {
            $(document).on('click','.del-item',function () {
                if($(".pd-item").length > 1){
                    $(this).parent().remove();
                }else {
                    $(this).remove();
                }
            });
            Controller.api.bindevent();
        },
        record:function () {
            var ids = $("#ids").val();
            Table.api.init({
                extend: {
                    record_index_url: 'warehouse/inventorylist/record?ids='+ids
                }
            });
            var table = $("#table");
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.record_index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {field: 'createtime', title: __('日期'),addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'instoragecount', title: __('数量'),operate:false},
                        {field: 'count', title: __('剩余'),operate:false},
                        {field: 'type', title: __('类型'),searchList:{1: "入库", 2: "出库"},formatter:Controller.api.formatter.checktype},
                        {field: 'warehouseid', title: __('仓库'),searchList:{1: "法国Macway-斯特拉斯堡仓库"}},
                        {field: 'orderId', title: __('（出/入）库单号'), operate:false}
                        ]
                ],
                search:false,
                showToggle: false,
                searchFormVisible:true,
                showExport: false,
                showColumns: false,
                pageSize: 5,
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                checkstatus: function (value, row, index,wms) {
                    return wms;

                },
                checktype: function (value, row, index) {
                    if(value === 1){
                        return '入库';
                    }else if(value === 2){
                        return '出库';
                    }

                },
            }
        }
    };
    return Controller;
});